//
//  RenrenManager.m
//  SNS
//
//  Created by  on 11-11-19.
//  Copyright (c) 2011年 __MyCompanyName__. All rights reserved.
//

#import "RenrenManager.h"
#import "TKAlertCenter.h"
#import "Reachability.h"

static RenrenManager *renrenManagerSingleton = nil;

@implementation RenrenManager

@synthesize renren = _renren;

+ (RenrenManager *)sharedManager {
    if (!renrenManagerSingleton) {
        renrenManagerSingleton = [[RenrenManager alloc] init];
    }
    return renrenManagerSingleton;
}

- (id) init {
    self = [super init];
    if (self) {
        self.renren = [[Renren newRenRen] retain];
    }
    return self;
}

- (void) dealloc {
    [_renren release];
    
    [super dealloc];
}

- (void) login {
    if (![[Reachability reachabilityForInternetConnection] isReachable]) {
        [[TKAlertCenter defaultCenter] postAlertWithMessage:@"没有网络！"];
        return;
    }
    
    if (![_renren isSessionValid]) {
		NSArray *permissions = [NSArray arrayWithObjects:
                                @"read_user_album",
                                @"status_update",
                                @"photo_upload",
                                @"publish_feed",
                                @"create_album",
                                @"operate_like",nil];
		[_renren authorizationWithPermisson:permissions
                                andDelegate:self];
	}
}

- (void) logout {
    [_renren logout:self];
}


- (void) updateStatus: (NSString *)newStatus 
             delegate:(id)aDelegate 
       finishCallback:(SEL)aFinishCallback 
         failCallback:(SEL)aFailCallback {
    if (![[Reachability reachabilityForInternetConnection] isReachable]) {
        [[TKAlertCenter defaultCenter] postAlertWithMessage:@"没有网络！"];
        return;
    }
    
    self.delegate = aDelegate;
    self.finishCallback = aFinishCallback;
    self.failCallback = aFailCallback;
    
    NSMutableDictionary *params = [NSMutableDictionary dictionaryWithCapacity:2];
    [params setObject:@"status.set" forKey:@"method"];
    [params setObject:newStatus forKey:@"status"];
    [_renren requestWithParams:params andDelegate:self];
}

- (void) sendImage:(UIImage *)image
           withMsg:(NSString *)msg 
          delegate:(id)aDelegate 
    finishCallback:(SEL)aFinishCallback 
      failCallback:(SEL)aFailCallback {
    if (![[Reachability reachabilityForInternetConnection] isReachable]) {
        [[TKAlertCenter defaultCenter] postAlertWithMessage:@"没有网络！"];
        return;
    }
    
    self.delegate = aDelegate;
    self.finishCallback = aFinishCallback;
    self.failCallback = aFailCallback;
    
    ROPublishPhotoRequestParam *imageParam = [[ROPublishPhotoRequestParam alloc] init];
    imageParam.imageFile = image;
    imageParam.caption = msg;
    [_renren publishPhoto:imageParam
              andDelegate:self];
}

#pragma mark - RenrenDelegate

- (void)renren:(Renren *)renren requestDidReturnResponse:(ROResponse*)response {
    
    if (self.delegate && [self.delegate respondsToSelector:self.finishCallback]) {
        [self.delegate performSelector:self.finishCallback withObject:response.rootObject];
    }
}

- (void)renren:(Renren *)renren requestFailWithError:(ROError*)error {
    NSLog(@"%@", [error description]);
    
	if (self.delegate && [self.delegate respondsToSelector:self.failCallback]) {
        [self.delegate performSelector:self.failCallback withObject:[error description]];
    }
}

- (void)renrenDidLogin:(Renren *)renren {
    [[TKAlertCenter defaultCenter] postAlertWithMessage:@"登陆成功！"
                                                  image:[UIImage imageNamed:@"renrenIcon.png"]];
}

- (void)renrenDidLogout:(Renren *)renren {
    [[TKAlertCenter defaultCenter] postAlertWithMessage:@"退出成功！"
                                                  image:[UIImage imageNamed:@"renrenIcon.png"]];
}

- (void)renren:(Renren *)renren loginFailWithError:(ROError*)error {

    NSLog(@"%@", [error localizedDescription]);
    
    [[TKAlertCenter defaultCenter] postAlertWithMessage:@"登陆失败！"
                                                  image:[UIImage imageNamed:@"renrenIcon.png"]];
}


@end
